//##############################################################################
//#    CuteWebshot is an application which can be used to render
//#    screenshots from webpages.
//#
//#    Copyright (C) 2010  Juhapekka Piiroinen
//#
//#    This program is free software: you can redistribute it and/or modify
//#    it under the terms of the GNU General Public License as published by
//#    the Free Software Foundation, either version 3 of the License, or
//#    (at your option) any later version.
//#
//#    This program is distributed in the hope that it will be useful,
//#    but WITHOUT ANY WARRANTY; without even the implied warranty of
//#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//#    GNU General Public License for more details.
//#
//#    You should have received a copy of the GNU General Public License
//#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
//#############################################################################
//# Contact: juhapekka.piiroinen@gmail.com
//#############################################################################
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QSharedMemory>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QBuffer>
#include <QTimer>
#include <QProcess>
#include <QList>
#include <QStringList>
#include <QApplication>
#include <QDir>
#include <QFile>
#include <QStringListModel>
#include <QFileDialog>
#include <QUrl>
#include <QLabel>
#include <QSpinBox>
#include <QGraphicsSimpleTextItem>
#include <QSettings>
#include <QThread>
#include <QSessionManager>
#include <QMessageBox>
#include <QSplashScreen>
#include <QScreen>
#include <QDesktopWidget>

#include "common.h"
#include "filetostringlistthread.h"

#define CHECK_BLOCKING_THREADS if (hasBlockingThreads()) { FUNC_OUT; return; }

namespace Ui {
    class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    void appendUrlToQueue(QString);
    void appendFailedUrl(QString);
    void appendProcessedUrl(QString);
    QString getNextUrlFromQueue();
    bool hasBlockingThreads();

public slots:
    void on_actionProcess_Next_triggered();
    void on_actionClear_Queue_triggered();
    void on_lineEditNewUrlToQueue_returnPressed();
    void on_lineEditTargetPath_returnPressed();
    void on_actionFrom_wget_to_queue_triggered();
    void on_actionFrom_csv_to_queue_triggered();
    void on_actionQueue_to_file_triggered();
    void on_actionQt_triggered() { QApplication::aboutQt(); }
    void on_actionExit_triggered() { this->close(); }

    void handleParallelProcessCountChanged(int);
    void handleAutomaticProcessing();
    void renderingComplete(int);
    void handleFileOpened(QStringList);
    void updateLabels();
    void commitData(QSessionManager&);
    void loadSettings();
    void saveSettings();

protected slots:
    virtual void closeEvent(QCloseEvent *);

private:
    Ui::MainWindow*         ui;
    QList<QProcess*>        m_renderingPipeline;
    QList<QTimer*>          m_renderingKillSwitch;
    QStringList             m_url;
    QStringListModel*       m_urlQueue;
    QStringListModel*       m_urlRendered;
    QStringListModel*       m_urlFailed;
    FileToStringListThread* m_fileOpenThread;
    QString                 m_webshotApp;
    bool                    m_bAboutToClose;
    QTimer*                 m_timerParallelProcessingChanged;

    //////////////////////
    // widgets for statusbar
    QLabel*                 m_labelUrlsInQueue;
    QLabel*                 m_labelCurrentlyProcessing;
    QLabel*                 m_labelProcessedUrls;
    QLabel*                 m_labelProcessingFailed;
    QSpinBox*               m_spinBoxParallelProcesses;

    ///////////////////////////
    // windows and dialogs
    QSplashScreen*           m_importSplash;

};

#endif // MAINWINDOW_H
